#include "BoundingBox.h"

HSMODEL_API  BoundingBox CalculateBoundingBox(const Model3D& i_model)
  {
  auto iterator = i_model.Points().begin();
  BoundingBox bbox;
  if (iterator == i_model.Points().end())
    {
    bbox.m_max_x = 0;
    bbox.m_max_y = 0;
    bbox.m_max_z = 0;
    bbox.m_min_x = 0;
    bbox.m_min_y = 0;
    bbox.m_min_z = 0;
    }
  else
    {
    bbox.m_max_x = (iterator)->X();
    bbox.m_max_y = (iterator)->Y();
    bbox.m_max_z = (iterator)->Z();
    bbox.m_min_x = (iterator)->X();
    bbox.m_min_y = (iterator)->Y();
    bbox.m_min_z = (iterator)->Z();
    ++iterator;
    for(iterator; iterator != i_model.Points().end(); ++iterator)
      {
      if ((iterator)->X() > bbox.m_max_x)//maxX
        {
        bbox.m_max_x = (iterator)->X();
        }
      if ((iterator)->X() < bbox.m_min_x)//minX
        {
        bbox.m_min_x = (iterator)->X();
        }
      if ((iterator)->Y() > bbox.m_max_y)//MaxY
        {
        bbox.m_max_y = (iterator)->Y();
        }
      if ((iterator)->Y() < bbox.m_min_y)//MinY
        {
        bbox.m_min_y = (iterator)->Y();
        }
      if ((iterator)->Z() > bbox.m_max_z)//MaxZ
        {
        bbox.m_max_z = (iterator)->Z();
        }
      if ((iterator)->Z() < bbox.m_min_z)//MinZ
        {
        bbox.m_min_z = (iterator)->Z();
        }
      }
    }
  return bbox;
  }
